#pragma once

#include <QCryptographicHash>
#include <QCoreApplication>
#include <QTcpSocket>
#include <QtMath>

//#include <QDebug>

#define APP_DIR QCoreApplication::applicationDirPath()

inline QByteArray Sha256(const QByteArray &array) {
    return QCryptographicHash::hash(array, QCryptographicHash::Sha256);
}

inline QString GetFileSize(qint64 size) {
    if (!size)
        return "0 Bytes";
    static QStringList SizeNames;
    if(SizeNames.empty()){
        SizeNames << " Bytes" << " KB" << " MB"
                  << " GB" << " TB" << " PB" << " EB" << " ZB" << " YB";
    }
    int i = qFloor(qLn(size) / qLn(1024));
    return QString::number(size * 1.0 / qPow(1024, qFloor(i)),
                           'f', (i > 1) ? 2 : 0 ) + SizeNames.at(i);
}

struct ReceiveData
{
    QByteArrayList dataList;
    QString xml;
};
inline ReceiveData parseReceiveData(const QByteArray &array) {
    ReceiveData receiveData;
    int ind = 0;
    while(ind < array.length() - 5) {
        if(array[ind] != '$')
            break;
        ind++;
        qint64 len = *reinterpret_cast<const qint64*>(array.data() + ind);
        ind += 8;
        receiveData.dataList << array.mid(ind, (int)len);
        ind += len;
    }
    receiveData.xml = array.mid(ind);
    return receiveData;
}

//inline void WaitForBytesWritten(QTcpSocket *socket) {
//    QObject object;
//    QEventLoop eventLoop;
//    object.connect(socket, &QTcpSocket::bytesWritten, &object, [&eventLoop] { eventLoop.quit(); });
//    QTimer::singleShot(2000, &object, [&eventLoop] { eventLoop.quit(); });
//    eventLoop.exec();
//}

